package xlcao.sohutv.ui;

import xlcao.sohutv.R;
import xlcao.sohutv.bean.Const;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.View;


public class UserDialogFragment extends DialogFragment {


	/**
	 * Create a new instance of MyDialogFragment, providing "type" as an
	 * argument.
	 */
	static UserDialogFragment newInstance(int type) {
		UserDialogFragment f = new UserDialogFragment();
		Bundle args = new Bundle();
		args.putInt("type", type);
		f.setArguments(args);
		return f;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		 int style = DialogFragment.STYLE_NORMAL, theme = 0;
		 setStyle(style, theme);
		int type = getArguments().getInt("type");
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		final FragmentManager mFm = getFragmentManager();
		switch (type) {

		case Const.DIALOG_CONFIRM_QUIT:

			builder.setMessage(R.string.QUIT_CONFIRM_MSG)
					.setCancelable(false)
					.setPositiveButton(R.string.QUIT,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									finishApp();
								}
							}).setNegativeButton(R.string.GOON_WATCH, null);
			return builder.create();

		case Const.DIALOG_CONFIRM_3G:

			builder.setMessage(R.string.USE_3G_CONFIRM_MSG)
					.setCancelable(false)
					.setPositiveButton(R.string.QUIT,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									finishApp();
								}
							}).setNegativeButton(R.string.CONTINUE, null);
			return builder.create();

		case Const.DIALOG_ERROR_RETRY:
			builder.setMessage(R.string.HAS_ERROR)
					.setCancelable(false)
					.setPositiveButton(R.string.RETRY,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									MeleTVViewerFragment viewer = (MeleTVViewerFragment) mFm
											.findFragmentById(R.id.tvview_fragment);
									viewer.reload();
								}
							}).setNegativeButton(R.string.RETURN, null);
			return builder.create();

		case Const.DIALOG_NO_NETWORK:
			builder.setMessage(R.string.NO_USEFUL_NETWORK).setPositiveButton(
					"OK", null);
			return builder.create();
		case Const.DIALOG_ABOUT:
			String message = getResources().getString(R.string.ABOUT_DETAIL);
			String msgFormatted = String.format(message, getVersionName());
			builder.setMessage(msgFormatted).setCancelable(false)
					.setNegativeButton(R.string.OK, null);
			return builder.create();
		default: break;
		}
		return super.onCreateDialog(savedInstanceState);
	}

	private void finishApp() {
		View img = getActivity().findViewById(R.id.tvview_fragment);
		img.startAnimation(new TVOffAnimation());

		Runnable mHideRunnable = new Runnable() {
			@Override
			public void run() {
				//getActivity().finish();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		};

		Handler mHideHandler = new Handler();
		mHideHandler.postDelayed(mHideRunnable, 500);
	}

	private String getVersionName() {
		PackageManager packageManager = getActivity().getPackageManager();
		PackageInfo packInfo;
		try {
			packInfo = packageManager.getPackageInfo(getActivity()
					.getPackageName(), 0);
			return packInfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "0.0";
	}
}